#include "common.hpp"
#include "xfactory.hpp"

#include <stdlib.h>
#include <signal.h>

int
main(int argc, char* argv[])
{
	if ( argc > 1 )
	{
		gPort = argv[1];

		if ( argc > 2 )
		{
			gPollType = argv[2];
		}
	}

	gPoll = PWIOPoll::create(gPollType);
	if ( !gPoll )
	{
		cerr << "failed to init poll: " << gPollType << endl;
		return EXIT_FAILURE;
	}

	if ( !gPoll->initialize(NULL) )
	{
		cerr << "failed to init poll: " << gPollType << endl;
		return EXIT_FAILURE;
	}

	XFactory factory;
	if ( !factory.open(gPort) )
	{
		cerr << "failed to open socket: " << gPort << endl;
		return EXIT_FAILURE;
	}

	gPoll->add(&factory, PWIO_R);

	signal(SIGPIPE, SIG_IGN);
	while ( gPoll->dispatch( 1000 ) )
	{
		factory.runRemove();
	}

	cerr << "good bye!" << endl;

	return EXIT_SUCCESS;
}

